﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoolCode.Caching
{
    // 定义如何添加、获取、删除欲进行缓存的对象
    public interface ICacheStrategy
    {
        // 添加对象
        void Insert(string key, object obj);

        // 获取对象
        object Get(string key);

        // 删除对象
        void Remove(string key);
    }

    /// <summary>
    /// 类型缓存
    /// </summary>
    public static class TypeCache
    {
        public static void AddItem(this ICacheStrategy cache, Type key, object obj)
        {
            cache.Insert(key.FullName, obj);
        }

        public static object GetItem(this ICacheStrategy cache, Type key)
        {
            return cache.Get(key.FullName);
        }

        public static void RemoveItem(this ICacheStrategy cache, Type key)
        {
            cache.Remove(key.FullName);
        }

        public static void AddItem<T>(this ICacheStrategy cache, object obj, string key)
        {
            cache.Insert(key + "/" + typeof(T).FullName, obj);
        }

        public static object GetItem<T>(this ICacheStrategy cache, string key)
        {
            return cache.Get(key + "/" + typeof(T).FullName);
        }

        public static void RemoveItem<T>(this ICacheStrategy cache, string key)
        {
            cache.Remove(key + "/" + typeof(T).FullName);
        }
    }
}
